import Game from "./game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class shoot extends cc.Component {
    private game:Game = undefined
    private ball:cc.Node = undefined
    private radian:number = 0
    private speed:number = 800

    // onLoad () {}
    init(game:Game,node:cc.Node){
        this.game = game
        this.ball = node
        this.radian = cc.misc.degreesToRadians(-this.ball.angle%360)

    }
    onCollisionEnter(other,self){
        this.game.setScore()
        this.removeShoot()
        this.game.isFirst = false
        this.game.randomBall()
        
    }

    private removeShoot(){
        this.node.destroy()
    }

    update (dt) {
        let bx = this.node.x
        let by = this.node.y
         bx += dt *this.speed * Math.sin(this.radian)
         by += dt * this.speed * Math.cos(this.radian)
        this.node.x = bx
        this.node.y = by
        if(this.node.x>cc.winSize.width/2 ||this.node.x < -cc.winSize.width/2|| this.node.y >cc.winSize.height/2 || this.node.y< -cc.winSize.height/2){
           this.game.init()
           this.game.isFirst = true
           this.removeShoot()
           this.game.randomBall()                     
        }
    }
}
